home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / dflat_r_.arc / DECOMP.C < prev    next >
Text File  |  1991-10-02  |  3KB  |  132 lines

  1. /* ------------------- decomp.c -------------------- */
  2.  
  3. /*
  4.  * Decompress the application.HLP file
  5.  * or load the application.TXT file if the .HLP file
  6.  * does not exist
  7.  */
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include "dflat.h"
  13. #include "htree.h"
  14.  
  15. #ifdef INCLUDE_COMPRESS_HELPFILE
  16.  
  17. static int in8;
  18. static int ct8 = 8;
  19.  
  20. static FILE *fi;
  21. static BYTECOUNTER bytectr;
  22.  
  23. static int LoadingASCII;
  24.  
  25. FILE *OpenHelpFile(void)
  26. {
  27.     unsigned char c;
  28.     char *cp;
  29.     int freqctr;
  30.     extern char **Argv;
  31.     char helpname[65];
  32.  
  33.     strcpy(helpname, Argv[0]);
  34.     cp = strrchr(helpname, '\\');
  35.     if (cp == NULL)
  36.         cp = helpname;
  37.     else 
  38.         cp++;
  39.     strcpy(cp, DFlatApplication);
  40.     strcat(cp, ".HLP");
  41.  
  42.     if ((fi = fopen(helpname, "rb")) == NULL)    {
  43.         strcpy(cp, DFlatApplication);
  44.         strcat(cp, ".TXT");
  45.         if ((fi = fopen(helpname, "rt")) == NULL)
  46.             return NULL;
  47.         LoadingASCII = TRUE;
  48.     }
  49.  
  50.     if (!LoadingASCII && ht == NULL)    {
  51.         if ((ht = calloc(256, sizeof(struct htree))) != NULL)    {
  52.             /* ----- read the byte count ------ */
  53.             fread(&bytectr, sizeof bytectr, 1, fi);
  54.             /* ----- read the frequency count ------ */
  55.             fread(&freqctr, sizeof freqctr, 1, fi);
  56.             /* -------- read the characters ---------- */
  57.             while (freqctr--)   {
  58.                 fread(&c, sizeof(char), 1, fi);
  59.                 ht[c].ch = c;
  60.                 fread(&ht[c].cnt, sizeof(BYTECOUNTER), 1, fi);
  61.             }
  62.             /* ---- build the huffman tree ----- */
  63.             buildtree();
  64.         }
  65.     }
  66.     return fi;
  67. }
  68.  
  69. void *GetHelpLine(char *line)
  70. {
  71.     int h;
  72.     if (LoadingASCII)
  73.         return fgets(line, 160, fi);
  74.     *line = '\0';
  75.     while (TRUE)    {
  76.         /* ----- decompress a line from the file ------ */
  77.         h = root;
  78.         /* ----- first get a character ----- */
  79.         while (ht[h].right != -1)    {
  80.             if (ct8 == 8)   {
  81.                 if ((in8 = fgetc(fi)) == EOF)    {
  82.                     *line = '\0';
  83.                     return NULL;
  84.                 }
  85.                 ct8 = 0;
  86.             }
  87.             if (in8 & 0x80)
  88.                 h = ht[h].left;
  89.             else
  90.                 h = ht[h].right;
  91.             in8 <<= 1;
  92.             ct8++;
  93.         }
  94.         if ((*line = ht[h].ch) == '\r')
  95.             continue;
  96.         if (*line == '\n')
  97.             break;
  98.         line++;
  99.     }
  100.     *++line = '\0';
  101.     return line;
  102. }
  103.  
  104. void HelpFilePosition(long *offset, int *bit)
  105. {
  106.     *offset = ftell(fi);
  107.     if (LoadingASCII)
  108.         *bit = 0;
  109.     else    {
  110.         if (ct8 < 8)
  111.             --*offset;
  112.         *bit = ct8;
  113.     }
  114. }
  115.  
  116. void SeekHelpLine(long offset, int bit)
  117. {
  118.     int i;
  119.     fseek(fi, offset, 0);
  120.     if (!LoadingASCII)    {
  121.         ct8 = bit;
  122.         if (ct8 < 8)    {
  123.             in8 = fgetc(fi);
  124.             for (i = 0; i < bit; i++)
  125.                 in8 <<= 1;
  126.         }
  127.     }
  128. }
  129.  
  130. #endif
  131.  
  132.